package vesion2.main;

import vesion2.tool.PlayMp3;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

public class ControlPanel extends JPanel{
    //敌人总数
    private JLabel la01;
    private JSlider js01;
    //同时存在最多敌人数
    private JLabel la02;
    private JSlider js02;
    //选择地图
    private JButton open;
    private JTextArea text;

    private JButton reStar;
    private JLabel split;
    private JButton keep;
    //消灭敌人总数
    private JLabel hasKill;
    //死亡次数
    private JLabel deathNUm;

    private GamePanel gPanel;
    //地图文档路径
    private String filePath=null;

    private JFrame frame;

    private PlayMp3 mp3;

    public ControlPanel() {
        super();
        mp3=new PlayMp3("src/main/resources/backgr.mp3");
        la01 = new JLabel("敌人总数: 5", SwingConstants.LEFT);
        js01=new JSlider(0,30,5);
        la02=new JLabel("最大同时存在敌人数: 2",SwingConstants.LEFT);
        js02=new JSlider(0,3,2);
        open=new JButton("选择文件");
        split=new JLabel();
        text=new JTextArea();
        reStar=new JButton("重新开始");
        keep=new JButton("继续游戏");
        hasKill=new JLabel("击杀数: 0",SwingConstants.LEFT);
        deathNUm=new JLabel("死亡数: 0",SwingConstants.LEFT);
        add(la01);
        add(js01);
        add(la02);
        add(js02);
        add(open);
        add(text);
        add(split);
        add(reStar);
        add(keep);
        add(hasKill);
        add(deathNUm);
        init();
    }

    private void init() {
        hasKill.setOpaque(true);
        hasKill.setFont(new Font(null, Font.PLAIN, 25));
        hasKill.setLocation(20, 30);
        hasKill.setSize(120, 40);

        deathNUm.setOpaque(true);
        deathNUm.setFont(new Font(null, Font.PLAIN, 25));
        deathNUm.setLocation(20, 90);
        deathNUm.setSize(120, 40);

        //最大敌人数
        la01.setOpaque(true);
        la01.setFont(new Font(null, Font.PLAIN, 20));
        la01.setLocation(20, 180);
        la01.setSize(120, 40);

        js01.setMajorTickSpacing(10);
        js01.setMinorTickSpacing(5);
        js01.setPaintLabels(true);
        js01.setPaintTicks(true);
        js01.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                la01.setText("敌人总数: " + String.valueOf(js01.getValue()));
                if (js01.getValue() > 20) {
                    js02.setMaximum(js01.getValue() / 3);
                    js02.setValue(5);
                } else {
                    if (js01.getValue() > 0) {
                        js02.setMaximum(js01.getValue()/2);
                        js02.setValue(js01.getValue() / 3);
                    } else {
                        js02.setMaximum(0);
                        js02.setValue(0);
                    }
                }
            }
        });
        js01.setLocation(20, 240);
        js01.setSize(200, 40);

        //最多人数
        la02.setOpaque(true);
        la02.setFont(new Font(null, Font.PLAIN, 20));
        la02.setLocation(20, 300);
        la02.setSize(220, 40);

        js02.setMajorTickSpacing(5);
        js02.setMinorTickSpacing(1);
        js02.setPaintLabels(true);
        js02.setPaintTicks(true);
        js02.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                la02.setText("最大同时存在敌人数: " + js02.getValue());
            }
        });
        js02.setLocation(20, 360);
        js02.setSize(200, 40);

        //选择文件
        open.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                filePath=showFileOpenDialog(ControlPanel.this,text);
                frame.requestFocus();
            }
        });
        open.setLocation(20,410);
        open.setSize(100,40);
        text.setLocation(20,470);
        text.setSize(260,28);
        text.setFont(new Font(null, Font.PLAIN, 22));

        reStar.setLocation(20,520);
        reStar.setSize(260,40);
        reStar.setFont(new Font(null, Font.PLAIN, 20));
        reStar.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gPanel.reStartGame(js01.getValue(),js02.getValue(),filePath);
                frame.requestFocus();
            }
        });

        split.setOpaque(true);
        split.setLocation(20,590);
        split.setSize(260,1);
        split.setBackground(Color.red);

        keep.setLocation(20,630);
        keep.setSize(260,40);
        keep.setFont(new Font(null, Font.PLAIN, 20));
        keep.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(!mp3.isStart())
                    mp3.startPlay(true);
               if(gPanel.isStop()) {
                   gPanel.keepGame();
                   mp3.keep();
                   keep.setText("暂停游戏");
               }
               else
               {
                   gPanel.pauseGame();
                   mp3.stopPlay();
                   keep.setText("继续游戏");
               }
               frame.requestFocus();

            }
        });
    }

    private String showFileOpenDialog(Component parent, JTextArea msgTextArea) {
        String pathFile=null;
        // 创建一个默认的文件选取器
        JFileChooser fileChooser = new JFileChooser();
        // 设置默认显示的文件夹为当前文件夹
        fileChooser.setCurrentDirectory(new File("."));
        // 设置文件选择的模式（只选文件、只选文件夹、文件和文件均可选）
        fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        // 设置是否允许多选
        fileChooser.setMultiSelectionEnabled(false);
        // 设置默认使用的文件过滤器
        fileChooser.setFileFilter(new FileNameExtensionFilter("json(*.json, *.json, *.json)", "json", "json", "json"));
        // 打开文件选择框（线程将被阻塞, 直到选择框被关闭）
        int result = fileChooser.showOpenDialog(parent);
        if (result == JFileChooser.APPROVE_OPTION) {
            // 如果点击了"确定", 则获取选择的文件路径
            File file = fileChooser.getSelectedFile();
            pathFile=file.getPath();
            int i1 = pathFile.lastIndexOf("\\");
            int i = pathFile.lastIndexOf(".json");
            if(pathFile.length()!=i+5) {
                pathFile = null;
                text.setText("打开错误");
            }
            else
                text.setText(pathFile.substring(i1+1,i+5));

        }
        return pathFile;
    }

    public void setPanel(GamePanel gPanel){
        this.gPanel=gPanel;
        gPanel.setControlPanel(this);
    }

    public void setJFrame(JFrame frame){
        this.frame=frame;
    }

    public JLabel getHasKill() {
        return hasKill;
    }

    public JLabel getDeathNUm() {
        return deathNUm;
    }

    public JSlider getJs01() {
        return js01;
    }

    public JSlider getJs02() {
        return js02;
    }

    public JButton getKeep() {
        return keep;
    }

    public void closeMusic(){
        mp3.close();
    }
}
